# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html

import pymysql

class XjnuPipeline(object):
    def process_item(self, item, spider):
        db = pymysql.connect(user='root', passwd='root',
                     host='localhost', db='xjnu', charset='utf8')
        cur = db.cursor()

        print('*'*60)
        print('lianjie cg')

        sql_insert = '''insert into data(title,data,content,annex_name,annex_url) values('{}','{}','{}','{}','{}');'''\
            .format(item['title'], item['data'], item['content'], item['annex_name'], item['annex_url'])
        try:
            cur.execute(sql_insert)
            db.commit()

            print('xieru cg')
        except Exception as e:
            # raise e
            db.rollback()
            print('error', e)
        finally:
            db.close()

        return item


